package com.zy.datareceiver.binsess.status.service.impl;

import com.zy.datareceiver.binsess.monitoring.entity.MonitoringPoint;
import com.zy.datareceiver.binsess.monitoring.mapper.MonitoringPointMapper;
import com.zy.datareceiver.binsess.status.entity.ObjectMonitoring;
import com.zy.datareceiver.binsess.status.entity.Passageway;
import com.zy.datareceiver.binsess.status.mapper.ObjectMonitoringMapper;
import com.zy.datareceiver.binsess.status.mapper.PassagewayMapper;
import com.zy.datareceiver.binsess.status.service.PassagewayService;
import com.zy.datareceiver.core.utils.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PassagewayServiceImpl implements PassagewayService {
    @Resource
    private PassagewayMapper passagewayMapper;
    @Resource
    private MonitoringPointMapper monitoringPointMapper;
    @Resource
    private ObjectMonitoringMapper objectMonitoringMapper;

    @Override
    public List<Passageway> selectAll() {
        return passagewayMapper.selectAll();
    }

    @Override
    public void saveConnectionException(Integer passagewayId) {
        List<Integer> ids = new ArrayList<>();
        switch (passagewayId){
            case 0:
                passagewayMapper.saveConnectionExceptionByType(passagewayId);
                List<Passageway> passagewayList = passagewayMapper.selectByType((short)0);
                for (Passageway passageway:passagewayList) {
                    ids.add(passageway.getId());
                }
                break;
            case -1://所有数据采集程序均断开
                ObjectMonitoring monitoring = new ObjectMonitoring();
                monitoring.setOperateTime(DateUtil.format(new Date(),DateUtil.PATTERN_YYYY_MM_DD_HHMM));
                monitoring.setMonitoringObject("所有数据采集程序");
                monitoring.setMonitoringType(0);
                monitoring.setStatus(1);
                monitoring.setRecover(0);
                objectMonitoringMapper.updateByObj(monitoring);
                break;
                default:
                    ids.add(passagewayId);
                    passagewayMapper.saveConnectionExceptionById(passagewayId);
        }
        //联动修改监测点链接状态
        monitoringPointMapper.changeStatusByPassId(ids);

        saveObjectMonitoringData(ids);
    }

    private void saveObjectMonitoringData(List<Integer> passagewayIds){
        List<MonitoringPoint> monitoringPoints = monitoringPointMapper.getMonitoringPointByPassId(passagewayIds);
        List<ObjectMonitoring> monitoringList = new ArrayList<>();
        //检测表状态存储
        for (MonitoringPoint point:monitoringPoints) {
            ObjectMonitoring monitoring = new ObjectMonitoring();
            monitoring.setOperateTime(DateUtil.format(new Date(),DateUtil.PATTERN_YYYY_MM_DD_HHMM));
            monitoring.setMonitoringObject(point.getPointName());
            monitoring.setMonitoringType(0);
            monitoring.setStatus(1);
            monitoring.setRecover(0);
            monitoringList.add(monitoring);
        }
        objectMonitoringMapper.batchUpdate(monitoringList);
    }

    @Override
    public void anConnectionException() {

    }
}
